package com.lftea.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.lftea.dao.IProductClassDAO;
import com.lftea.po.ProductClass;
import com.lftea.util.StringUtils;

@Repository("productClassDAOImpl")
public class ProductClassDAOImpl implements IProductClassDAO {

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	public List<ProductClass> findAll() {
		String sql = "select * from ProductClass";
		return jdbcTemplate.query(sql, new ProductClassMapper());
	}

	public ProductClass findById(long productClassId) {
		String sql = "select * from ProductClass where Product_ID=?";
		return (ProductClass) jdbcTemplate.query(sql,
				new Object[] { productClassId }, new ProductClassMapper());
	}

	public void remove(ProductClass productClass) {
		long productClassId = productClass.getId();
		String sql = "delete from ProductClass where ProductClass_ID=?";
		jdbcTemplate.update(sql, new Object[] { productClassId });
	}

	public void save(ProductClass productClass) {
		long productClassId = productClass.getId();
		String name = StringUtils.toISO(productClass.getName());
		String createDate = productClass.getCreateDate();
		System.out.println("save() productClassId="+productClassId);

		String sql = "select count(*) from ProductClass where ProductClass_ID=?";
		int result = jdbcTemplate.queryForInt(sql,
				new Object[] { productClassId });
		Object[] params = null;
		if (result > 0) {
			sql = "update ProductClass set Name=?,CreateDate=? where ProductClass_ID=?";
			params = new Object[] { name, createDate, productClassId };
		} else {
			sql = "insert into ProductClass(Name,CreateDate) values(?,?)";
			params = new Object[] { name, createDate };
		}
		jdbcTemplate.update(sql, params);
	}

	@Override
	public void add(ProductClass productClass) {
		System.out.println("\n添加：" + productClass.getName() + ","
				+ productClass.getCreateDate());
		save(productClass);
	}

	private static final class ProductClassMapper implements
			RowMapper<ProductClass> {

		public ProductClass mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			ProductClass productClass = new ProductClass();
			productClass.setId(rs.getLong("ProductClass_ID"));
			productClass.setName(StringUtils.toGBK(rs.getString("name")));
			productClass.setCreateDate(String.valueOf(rs.getObject("CreateDate")));
			return productClass;
		}

	}

	@Override
	public List<ProductClass> find(ProductClass productClass) {		
		String sql = "select * from ProductClass";
		long productClassId = productClass.getId();
		String name = StringUtils.toISO(productClass.getName());
		String createDate = productClass.getCreateDate();
		
		StringBuffer sqlWheres = new StringBuffer();
		List<Object> paramList = new ArrayList<Object>();
		if(productClassId != 0){
			sqlWheres.append(" And ProductClass_ID=?");
			paramList.add(productClassId);
		}
		if(null != name){
			sqlWheres.append(" And Name=?");
			paramList.add(productClassId);
		}
		if(null != createDate){
			sqlWheres.append(" And CreateDate=?");
			paramList.add(createDate);			
		}
		
		if(sqlWheres.length() > 0){
			sql = sql + " where " + sqlWheres.substring(sqlWheres.indexOf("and"));			
		}
		
		Object[] params = paramList.toArray();
		
		return jdbcTemplate.query(sql, params, new ProductClassMapper());
	}
}
